//
//  RW_MixedAudioView.m
//  SleepApp
//
//  Created by MAC on 2021/3/22.
//  Copyright © 2021 RiceWang. All rights reserved.
//

#import "RW_MixedAudioView.h"
#import "RW_AVAudioPlayerTools.h"
#import "RW_Players.h"
#import "RW_PushPlayersTools.h"

@interface RW_MixedAudioView ()
/** 播放器 */
@property (nonatomic,strong) RW_Players *wlclass_players_model;

/** 名称 */
@property (nonatomic,strong) UILabel *title_label;
/** 播放进度文本 */
@property (nonatomic,strong) UILabel *play_progress_label;
/** 播放音量修改 */
@property (nonatomic,strong) UISlider *play_volume_slider;
/** 播放暂停按钮 */
@property (nonatomic,strong) UIButton *play_stop_button;
/** 当前播放状态 */
@property (nonatomic,strong) UILabel *play_status_label;
/** 加载进度 */
@property (nonatomic,strong) UIProgressView *progress_view;

@end
@implementation RW_MixedAudioView
- (instancetype)init
{
    self = [super init];
    if (self) {
        self.backgroundColor = UIColor.randomColor;
        [self addSubview:self.title_label];
        [self addSubview:self.play_progress_label];
        [self addSubview:self.play_stop_button];
        [self addSubview:self.play_volume_slider];
        [self addSubview:self.play_status_label];
        [self addSubview:self.progress_view];
        [self.title_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self).mas_offset(5);
            make.top.mas_equalTo(self).mas_offset(5);
        }];
        [self.play_progress_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self).mas_offset(-5);
            make.top.mas_equalTo(self).mas_offset(5);
        }];
        [self.play_stop_button mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self);
            make.top.mas_equalTo(self).mas_offset(5);
        }];
        [self.play_volume_slider mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self).mas_offset(-5);
        }];
        [self.progress_view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.mas_equalTo(self);
            make.bottom.mas_equalTo(self);
        }];
        [self.play_status_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self).mas_offset(5);
            make.left.mas_equalTo(self.title_label.mas_right).mas_offset(5);
        }];
    }
    return self;
}
#pragma mark -- 属性设置
/** 设置属性 */
-(void)setAudio_model:(RW_MixedAudioModel *)audio_model{
    _audio_model = audio_model;
    if (_audio_model.song_model){
        [self.wlclass_players_model setSongList:@[_audio_model.song_model]];
        [self.wlclass_players_model change_play_index:0];
        [self initPlayParams:_audio_model];
    }
}
/** 播放参数初始化 */
-(void)initPlayParams:(RW_MixedAudioModel *)model{
    //声音大小初始化
    [self changeVolumeValue:model.volume];
    //音频名初始化
    self.title_label.text = model.song_model.name;
}
/** 设置播放器属性监听 */
-(void)init_players_model:(RW_Players *)players_model{
    __WEAK_SELF_YLSLIDE
    /** 当前播放的音频总秒数 只读 */
    [RACObserve(players_model, play_all_second) subscribeNext:^(id play_all_second_t) {
        NSInteger play_all_second = [play_all_second_t integerValue];
        if (play_all_second){
            [weakSelf change_progress_time:0 allTime:play_all_second];
        }
    }];
    /** 当前播放的音频的秒数 修改当前播放的进度 读写 */
    [RACObserve(players_model, play_second) subscribeNext:^(id play_second_t) {
        NSInteger play_second = [play_second_t integerValue];
        if (play_second){
            [weakSelf change_progress_time:play_second allTime:players_model.play_all_second];
        }
    }];
    /** 当前播放状态 只读 */
    [RACObserve(players_model, play_status_type) subscribeNext:^(id play_status_type_t) {
        Player_Status_Type play_status_type = (Player_Status_Type)[play_status_type_t intValue];
        NSLog(@"当前状态：%lu",(unsigned long)play_status_type);
        [weakSelf change_play_status:play_status_type];
    }];
    /** 当前下载进度  play_status_type 为 Player_Status_Type_Download 才有效  只读 */
    [RACObserve(players_model, play_download_progress) subscribeNext:^(id play_download_progress_t) {
        float play_download_progress = [play_download_progress_t floatValue];
        if (play_download_progress){
            NSLog(@"下载进度：%f",play_download_progress);
            dispatch_async(dispatch_get_main_queue(), ^{
                [weakSelf.progress_view setProgress:play_download_progress animated:true];
            });
        }
    }];
   
}

#pragma mark -- 被动显示功能
/** 修改显示的当前播放进度 */
-(void)change_progress_time:(NSInteger )time allTime:(NSInteger)allTime{
    self.play_progress_label.text = [NSString stringWithFormat:@"%ld:%ld",time,allTime];
}
/** 修改当前的播放状态文本 */
-(void)change_play_status:(Player_Status_Type )play_status_type{
    switch (play_status_type) {
        case Player_Status_Type_Default:{//等待播放
            [self.play_stop_button setTitle:@"播放" forState:UIControlStateNormal];
        }
            break;
        case Player_Status_Type_Stoping:{//暂停
            [self.play_stop_button setTitle:@"播放" forState:UIControlStateNormal];
        }
            break;
        case Player_Status_Type_Playing:{//正在播放
            [self.play_stop_button setTitle:@"暂停" forState:UIControlStateNormal];
            [self.progress_view setProgress:1 animated:true];
        }
            break;
        case Player_Status_Type_Download:{//下载状态
            [self.play_stop_button setTitle:@"下载中" forState:UIControlStateNormal];
            [self.progress_view setProgress:0 animated:false];
        }
            break;
        case Player_Status_Type_Error:{//播放失败或者下载失败
            [self.play_stop_button setTitle:@"重新下载" forState:UIControlStateNormal];
            [self.progress_view setProgress:0 animated:true];
        }
            break;
        default:
            [self.play_stop_button setTitle:@"奇怪的状态" forState:UIControlStateNormal];
            break;
    }
}

#pragma mark -- 主动操作设置
/** 修改声音大小 */
-(void)sliderVolumeDidChanged:(UISlider *)slider{
    [self changeVolumeValue:slider.value];
}
-(void)changeVolumeValue:(NSInteger)volume{
    self.play_volume_slider.value = volume;
    [self.wlclass_players_model change_play_volume:volume];
}
/** 播放或者暂停 */
-(void)playClickButton:(UIButton *)sender{
    switch (self.wlclass_players_model.play_status_type) {
        case Player_Status_Type_Default://等待播放
        case Player_Status_Type_Stoping:{//暂停
            //将单利播放器替换为当前的播放器
            if (![self.roowViewController isEqual:[RW_MixedViewController sharedInstance]]){
                //点击开始播放的时候停止其他播放器的播放状态
                [RW_PushPlayersTools stopOtherPlay];
                //将当前播放器替换为主播放器
                [RW_MixedViewController realodMixedViewController:self.roowViewController];
            }
            //开始播放内容
            [self.wlclass_players_model beginPlay];
        }
            break;
        case Player_Status_Type_Playing:{//正在播放
            //暂停播放
            [self.wlclass_players_model stopPlay];
        }
            break;
        case Player_Status_Type_Error:{//播放失败或者下载失败
            //开始播放
            [self.wlclass_players_model beginPlay];
        }
            break;
        case Player_Status_Type_Download://下载状态
        default:
            break;
    }
}
#pragma mark -- 主动操作功能
/** 停止播放 */
-(void)stopPlay{
    //暂停播放
    [self.wlclass_players_model stopPlay];
}
/** 释放当前播放器属性 */
-(void)releaseVM{
    [_wlclass_players_model releaseVM];
    _roowViewController = nil;
    _audio_model = nil;
    _wlclass_players_model = nil;
}



#pragma mark -- 懒加载
-(RW_Players *)wlclass_players_model{
    if (!_wlclass_players_model){
        _wlclass_players_model = [[RW_Players alloc]init];
        [self init_players_model:_wlclass_players_model];
    }
    return _wlclass_players_model;
}
-(UILabel *)title_label{
    if (!_title_label){
        _title_label = [[UILabel alloc]init];
        _title_label.backgroundColor = UIColor.randomColor;
    }
    return _title_label;
}
-(UILabel *)play_progress_label{
    if (!_play_progress_label){
        _play_progress_label = [[UILabel alloc]init];
        _play_progress_label.backgroundColor = UIColor.randomColor;
    }
    return _play_progress_label;
}
-(UIButton *)play_stop_button{
    if (!_play_stop_button){
        _play_stop_button = [[UIButton alloc]init];
        _play_stop_button.backgroundColor = UIColor.randomColor;
        [_play_stop_button addTarget:self action:@selector(playClickButton:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _play_stop_button;
}
-(UILabel *)play_status_label{
    if (!_play_status_label){
        _play_status_label = [[UILabel alloc]init];
        _play_status_label.backgroundColor = UIColor.randomColor;
    }
    return _play_status_label;
}
-(UISlider *)play_volume_slider{
    if (!_play_volume_slider){
        _play_volume_slider = [[UISlider alloc]init];
        _play_volume_slider.minimumValue = 1;
        _play_volume_slider.maximumValue = 100;
        [_play_volume_slider addTarget:self action:@selector(sliderVolumeDidChanged:) forControlEvents:UIControlEventValueChanged];
    }
    return _play_volume_slider;
}
-(UIProgressView *)progress_view{
    if (!_progress_view){
        _progress_view = [[UIProgressView alloc]init];
    }
    return _progress_view;
}
@end
